//
//  CZStatusViewModel.swift
//  sinaSwifWeiBo
//
//  Created by 张雪雪 on 16/3/3.
//  Copyright © 2016年 张雪雪. All rights reserved.
//

import UIKit



class CZStatusViewModel: NSObject {
   
    //viewMode里放Model
    var statusModel: CZStatusModel?
    /// 认证类型 -1：没有认证，0，认证用户，2,3,5: 企业认证，220: 达人
    /// verified_type
    
    var verifity_image:UIImage?{
    

        switch statusModel?.user?.verified_type ?? -1 {
        
        case -1 : return nil
        case 0: return UIImage(named: "avatar_vip")
        case 2,3,5: return UIImage(named: "avatar_enterprise_vip")
        case 220 : return UIImage(named: "avatar_grassroot")
        //switch语言需要加默认的 default
        default: return nil
        
        }
        
    }
    //会员等级
    
    var level_image:UIImage?{

        return UIImage(named: "common_icon_membership_level\(statusModel?.user?.mbrank ?? 0)")
    
    }
    //转发/评论/点赞
    var retweetString:String?{
        
        if statusModel?.reposts_count == 0{
            return "转发"
        
        }else{
    
                if let str = statusModel?.reposts_count{
                
                   return "\(str)"
                
                }
    
                return "转发"

        }
    
    }
    
    var commentString:String?{
    
    
        if statusModel?.comments_count == 0{

            return "评论"
        
        }else{
        
        
            if let str = statusModel?.comments_count {
            
            
               return "\(str)"
            
            }
           
        return "转发"
    
        }
    }
    
    var attitudesString:String?{
        
        
        if statusModel?.attitudes_count == 0{
            
            return "赞"
            
        }else{
            
            
            if let str = statusModel?.attitudes_count {
                
                
                return "\(str)"
                
            }
            
            return "转发"
            
        }
    }

    
    //MARK: 转发微博的内容
    var retweetStatusString: String? {
    
    
       return statusModel?.retweeted_status?.text 
    
    }
    
    func dealSource(sourceString:String?) -> String?{
    
    //进行一个判断, sourceString不能为空
        guard let source = sourceString else {
        
           return nil
        
        }
    //分割字符串 -> 
        
        let sourceArray = source.componentsSeparatedByString(">")
    // 严谨一定要考虑到其他的因素
        
        if sourceArray.count >= 2{
        
        let subSource = sourceArray[1]
            
            //分割字符串
            let lastSourceArray = subSource.componentsSeparatedByString("<")
            
            if lastSourceArray.count >= 1{
                
                return lastSourceArray.first
                
                }else{
                
                    return nil
            
            }
            
            
        }else{
        
          return nil
        
        }
  
  }
    
    
    func dealCreateDate(create_date:String?) -> String? {
    
    //1.获取当前的日历
        
        guard let createDate = create_date else {
 
            return nil
        
        }
        
        let formatter = NSDateFormatter()
    //注意设置时区
        formatter.locale = NSLocale(localeIdentifier: "cn")
    
        formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
        
        let date = formatter.dateFromString(createDate)
        
        //获取当前的时间
        let calendar = NSCalendar.currentCalendar()
        
        let unites = NSCalendarUnit(arrayLiteral: .Year,.Month,.Day,.Hour,.Minute)
        
        
        
        if let Odate = date{
        
                    let compts = calendar.components(unites, fromDate: Odate, toDate: NSDate(), options: [])
        
        
        if compts.year == 0 {
            //今年
            if compts.month == 0 { // ==0 是这个月
                
                if compts.day == 0{ // 今天
                    
                    if compts.hour == 0{ // 1小时之内
                        
                        if compts.minute == 0{ // 刚刚
                            
                            return "刚刚"
                        }else { // 几分钟之前
                            return "\(compts.minute)分钟前"
                        }
                        
                    } else { // 1小时之外
                        return "\(compts.hour)小时前"
                    }
                    
                } else if compts.day == 1 { // 昨天
                    // 几点几分  2016-03-04 09:50:27
                    
                    
                    formatter.dateFormat = "HH:mm"
                    let str = formatter.stringFromDate(Odate)
                    return "昨天 \(str)"
                }
                
            } else {
                // 其他月 "MM-dd HH:mm"
                formatter.dateFormat = "MM-dd HH:mm"
                let str = formatter.stringFromDate(Odate)
                return "昨天 \(str)"
            }
        } else {
            //其他年 "MM-dd HH:mm"
            formatter.dateFormat = "MM-dd HH:mm"
            let str = formatter.stringFromDate(Odate)
            return "昨天 \(str)"
        }
        
    }
    
    
    
    return "\(date)"
}








//
//    init(model : CZStatusModel) {
//
//        statusModel = model
//    }


}
